import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Window 2.0
import QtQuick.Dialogs 1.0
import QtQuick.XmlListModel 2.0

ApplicationWindow {
    title: qsTr("DTwenty Campaign Viewer")
    width: 1200
    height: 800

    /// \todo Use real data model instead of read only XML list model
    /// \author Jussi
    /// \date 2013-10-21
    XmlListModel {
        id: masterModel
        query: "/Campaign"
    }


    ModelSpecialAbilities {
        id: modelSpecialAbilities
        source: masterModel.source
    }


    ListModel {
        id: modelAttributes
        ListElement { attributeName: "Strength" }
        ListElement { attributeName: "Dexterity" }
        ListElement { attributeName: "Constitution" }
        ListElement { attributeName: "Intelligence" }
        ListElement { attributeName: "Wisdom" }
        ListElement { attributeName: "Charisma" }
    }

    ListModel {
        id: modelHitDice
        ListElement { dieValue: 4 }
        ListElement { dieValue: 6 }
        ListElement { dieValue: 8 }
        ListElement { dieValue: 10 }
        ListElement { dieValue: 12 }
    }

    ListModel {
        id: modelSkillPoints
        ListElement { skillPointName: "Uneducated"; skillPoints: 2 }
        ListElement { skillPointName: "Educated"; skillPoints: 4 }
        ListElement { skillPointName: "Expert"; skillPoints: 6 }
        ListElement { skillPointName: "Master"; skillPoints: 8 }
    }

    ListModel {
        id: modelBaseBonus
        ListElement { baseBonusName: "Poor"; baseBonus: 0 }
        ListElement { baseBonusName: "Normal"; baseBonus: 1 }
        ListElement { baseBonusName: "Good"; baseBonus: 2 }
    }

    ListModel {
        id: modelSaveBonus
        ListElement { saveBonusName: "Normal"; saveBonus: 0 }
        ListElement { saveBonusName: "Good"; saveBonus: 1 }
    }

    Action {
        id: actionNew
        text: qsTr("&New")
        shortcut: "CTRL+N"
        onTriggered: {
            if (masterModel.xml.length)
            {
                actionSave.trigger();
            }

            // load plain model from template/resorces???
        }
    }

    Action {
        id: actionOpen
        text: qsTr("&Open")
        shortcut: "CTRL+O"
        onTriggered: {
            if (masterModel.xml.length)
            {
                actionSave.trigger();
            }


            fileDialogOpen.open();
        }
    }

    Action {
        id: actionSave
        text: qsTr("&Save")
        shortcut: "CTRL+S"
        onTriggered: {
            fileDialogSave.open();
        }
    }

    Action {
        id: actionQuit
        text: qsTr("&Quit")
        shortcut: "CTRL+Q"
        onTriggered: Qt.quit();
    }

    Action {
        id: actionAddSkill
        text: qsTr("Add S&kill")
        shortcut: "CTRL+K"
        onTriggered: {
            /// \todo Add new item to skills model
            /// \author Jussi
            /// \date 2013-10-21

            // change page to skills
            leftPane.currentSelection = -1;
            contentLoader.state = "skills"

            /// \todo Set created item as current item in skills table page
            /// \author Jussi
            /// \date 2013-10-21
        }
    }

    Action {
        id: actionAddClass
        text: qsTr("Add Cha&racter Class")
        shortcut: "CTRL+R"
        onTriggered: {
            /// \todo Add new item to charactes model and set created item as current selection
            /// \author Jussi
            /// \date 2013-10-21

            leftPane.currentSelection = -1;
            contentLoader.state = "character"
        }
    }


    menuBar: MenuBar {
        Menu {
            title: qsTr("&File")
            MenuItem { action: actionNew }
            MenuItem { action: actionOpen }
            MenuItem { action: actionSave }
            MenuSeparator {}
            MenuItem { action: actionQuit }
        }

        Menu {
            title: qsTr("&Edit")
            MenuItem { action: actionAddSkill }
            MenuItem { action: actionAddClass }
        }
    }

    Rectangle {
        id: leftPane
        color: "steelblue"
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        width: 160

        property int currentSelection: -1

        onCurrentSelectionChanged: {
            if (contentLoader.status == Loader.Ready && currentSelection >= 0) {
                contentLoader.item.editIndex = currentSelection;
            }
        }


        Column {
            anchors.fill: parent
            anchors.margins: 6
            spacing: 6
            Text {
                text: "Core Features"
                color: "white"
                anchors.left: parent.left
                anchors.right: parent.right
                font.pixelSize: 16
                font.bold: true
            }

            Column {
                id: coreFeaturesMenu
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.margins: 2
                spacing: 2

                ComponentButton {
                    id: menuButton
                    text: "Skill Table"
                    onClicked: {
                        leftPane.currentSelection = -1;
                        contentLoader.state = "skills";
                        /// \todo show skill table design form on the left
                        /// \author Jussi
                        /// \date 2013-10-09
                    }
                }

                /// \todo button to open feat, spell, bonus etc. design form for campaign core features
                /// \author Jussi
                /// \date 2013-10-09
            }

            Text {
                text: "Character Classes"
                color: "white"
                anchors.left: parent.left
                anchors.right: parent.right
                font.pixelSize: 16
                font.bold: true
            }

            Column {
                id: characterClassMenu
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.margins: 2
                spacing: 2


                Repeater {
                    model: characterClassesModel
                    delegate: ComponentButton {
                        text: characterClass.name
                        onClicked: {
                            leftPane.currentSelection = index;
                            contentLoader.state = "character";
                            /// \todo show character class design form on the left
                            /// \author Jussi
                            /// \date 2013-10-09
                        }
                    }
                }
            }



        }

    }


    Loader {
        id: contentLoader
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.left: leftPane.right
        anchors.right: parent.right

        state: "skills"

        states: [
            State {
                name: "skills"
                PropertyChanges {
                    target: contentLoader
                    source: "PageSkills.qml"
                }
            },

            State {
                name: "character"
                PropertyChanges {
                    target: contentLoader
                    source: "PageCharacterClasses.qml"
                }
            }
        ]

        onLoaded: {
            if (leftPane.currentSelection >= 0) {
                contentLoader.item.editIndex = leftPane.currentSelection;
            }
        }
    }


    /// \todo Load model content.
    /// \author Jussi
    /// \date 2013-10-21
    FileDialog {
        id: fileDialogOpen
        nameFilters: [ "Xml files (*.xml)", "All files (*)" ]
        onAccepted: {
            masterModel.source = fileUrl;
        }
    }

    /// \todo Save model content.
    /// \author Jussi
    /// \date 2013-10-21
    FileDialog {
        id: fileDialogSave
        nameFilters: [ "Xml files (*.xml)", "All files (*)" ]
        onAccepted: {
            console.log("TODO: Save model content!");
        }
    }

    Component.onCompleted: {
        visible = true;

    }

}
